import {reactive,ref} from 'vue'
import {defineStore} from 'pinia'
export const useWaterData = defineStore('water',()=>{
  const title = ['ph值','溶解氧','水温']

  let area = ['地块1','地块2','地块3']
  let chooseArea = ref('地块1')

  let time = ref('day')

  function getData(length,begin,end){
    let res = []
    for(let i = 0;i<length;i++){
      res[i] = Math.floor(Math.random()*(end-begin + 1)) +begin
    }
    return res
  }
  const getAllChartData = ()=>{
    let res = {
      地块1:{
        day:[getData(50,0,20),getData(50,0,20),getData(50,0,20),getData(50,0,20),getData(50,0,20),getData(50,0,20),getData(50,0,20),],
        week:[getData(50,0,20),getData(50,0,20),getData(50,0,20),getData(50,0,20),getData(50,0,20),getData(50,0,20),getData(50,0,20),],
        month:[getData(50,0,20),getData(50,0,20),getData(50,0,20),getData(50,0,20),getData(50,0,20),getData(50,0,20),getData(50,0,20),]
    }
    ,
    地块2:{
      day:[getData(50,0,20),getData(50,0,20),getData(50,0,20),getData(50,0,20),getData(50,0,20),getData(50,0,20),getData(50,0,20),],
      week:[getData(50,0,20),getData(50,0,20),getData(50,0,20),getData(50,0,20),getData(50,0,20),getData(50,0,20),getData(50,0,20),],
      month:[getData(50,0,20),getData(50,0,20),getData(50,0,20),getData(50,0,20),getData(50,0,20),getData(50,0,20),getData(50,0,20),]
    },
      地块3:{
      day:[getData(50,0,20),getData(50,0,20),getData(50,0,20),getData(50,0,20),getData(50,0,20),getData(50,0,20),getData(50,0,20),],
      week:[getData(50,0,20),getData(50,0,20),getData(50,0,20),getData(50,0,20),getData(50,0,20),getData(50,0,20),getData(50,0,20),],
      month:[getData(50,0,20),getData(50,0,20),getData(50,0,20),getData(50,0,20),getData(50,0,20),getData(50,0,20),getData(50,0,20),]
      }
    }

    
    return res
  }
  const datas = reactive(getAllChartData())

 //获取x轴时间
 function getDay(date){
  let hour = String(new Date(date).getHours()).padStart(2, '0');  
  let minute = String(new Date(date).getMinutes()).padStart(2, '0'); 
  let second = String(new Date(date).getSeconds()).padStart(2, '0'); 
  return hour+':'+minute
}

function getWeek(date){
  return new Date(date).getDay()
}

function getMonth(date){
  return new Date(date).getDate() + '日'
}

const weekDay = ['周日', '周一', '周二', '周三', '周四', '周五', '周六']
let length = ref(0)

function getLastTime(time){
  let i = new Date()
  let res = []
  //当天
  if(time==='day'){    
  do{
    length.value++
    res.unshift(getDay(i))
    i-=3600000
  }while(!(String(new Date(i).getHours()).padStart(2, '0')==='23'))
  }
  
  //当周
  else if(time==='week'){
    let flag = 0
    do{
      flag++
      length.value++
      res.unshift(weekDay[getWeek(i)])
      i-=86400000
    }while(flag!==7)
  }
  //当月
  else if(time === 'month'){
    do{
      length.value++
      res.unshift(getMonth(i))
      i-=86400000
    }while(!(String(new Date(i).getDate())==='1'))
      res.unshift(getMonth(i))
  }
  return res
}

  return {title,area,chooseArea,datas,time,getLastTime}
})